package com.fish.gaiety.gateway.base;

import com.fish.gaiety.gateway.security.service.DBDecryption;
import com.fish.gaiety.gateway.security.service.DBEncryption;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.persistence.AttributeConverter;

@Component
@Slf4j
@RequiredArgsConstructor
public class SecurityEncryptionAndDecryptionConvert implements AttributeConverter<String, String> {

    private final DBEncryption dbEncryption;

    private final DBDecryption dbDecryption;

    @Override
    public String convertToDatabaseColumn(String attribute) {
        log.info("转换前：{} 转换后：{}", attribute, dbEncryption.encrypt(attribute));
        return dbEncryption.encrypt(attribute);
    }

    @Override
    public String convertToEntityAttribute(String dbData) {
        log.info("转换前：{} 转换后：{}", dbData, dbDecryption.decrypt(dbData));
        return dbDecryption.decrypt(dbData);
    }
}
